<template>
  <div>
    <el-button type="primary" @click="addswiperFn" v-addadmindirective="role">
      添加轮播图
      <el-icon class="el-icon--right">
        <Plus />
      </el-icon>
    </el-button>

    <!-- table 数据 -->

    <el-table :data="zongData" style="width: 100%">
      <el-table-column label="序号" width="100">
        <template #default="scope">
          <div
            style="display: flex; flex-direction: column; align-items: center"
          >
            <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片" width="180">
        <template #default="scope">
          <el-image :src="scope.row.img"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="链接" width="180">
        <template #default="scope">
          <span>{{ scope.row.link }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提示" width="180">
        <template #default="scope">
          <span>{{ scope.row.alt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template></el-table-column
      ></el-table
    >
    <!-- 分页布局代码 -->

    <el-pagination
      background
      layout="prev, pager, next"
      v-model:current-page="currentpage"
      :total="tableData.length"
    />
  </div>
</template>

<script>
import { getswiperlist } from "@/api/swiperapi.js";
import { Plus } from "@element-plus/icons-vue";
import { delswiperlist } from "@/api/swiperapi";
import { mapState } from "vuex";

// import { get } from "lodash";
export default {
  name: "",
  data() {
    return {
      tableData: [], //轮播图列表数局
      currentpage: 1,
    };
  },
  created() {
    this.getlist();
  },
  components: {
    Plus,
  },
  methods: {
    //// await 等待 等待结束后
    // ,其实就是等待then((res)=>{}) 该then 方法中的函数执行完毕,
    // 将值返回给await 的结果
    async getlist() {
      const res = await getswiperlist();
      console.log(res);
      this.tableData = res.data.data;
    },
    addswiperFn() {
      //添加轮播图
      this.$router.push("/home/swipermanage/addswiper");
    },
    handleDelete(index, row) {
      delswiperlist({ bannerid: row.bannerid }).then((res) => {
        if (res.data.code == 200) {
          this.getlist();
        }
      });
    },
  },
  computed: {
    ...mapState(["role"]),
    zongData() {
      return this.tableData.slice(
        (this.currentpage - 1) * 10,
        this.currentpage * 10
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}
</style>